﻿using System;
using System.Collections.Generic;
using System.Web;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;
using OneMG.Phoenix.Kernel.Util;
using OneMG.Phoenix.Kernel.Enum;
namespace OneMG.Phoenix.AdminSite.Views.Passport
{
    public class RightsSave : IHttpHandler
    {
        IRightsService IRights = new RightsService();

        public void ProcessRequest(HttpContext context)
        {
            int actionType = 0;
            int.TryParse(context.Request.Form["Action"], out actionType);
            string action = "";

            switch ((ActionType)actionType)
            {
                case ActionType.Insert:
                    {
                        Add(context);
                        action = "添加成功";
                    }
                    break;
                case ActionType.Delete:
                    {
                        delete(context);
                        action = "删除成功";
                    }
                    break;
                case ActionType.Modify:
                    {
                        update(context);
                        action = "修改成功";
                    }
                    break;

            }

            JsonResult.Result(context, JsonMessageStatus.OK, action);

        }

        private void Add(HttpContext context)
        {

            RightsModel model = new RightsModel()
            {
                RightDisplayName = HttpContext.Current.Server.UrlDecode(context.Request.Form["RightDisplayName"]),
                RightMemo = HttpContext.Current.Server.UrlDecode(context.Request.Form["RightMemo"]) ?? "",
                RightName = HttpContext.Current.Server.UrlDecode(context.Request.Form["RightName"]),
                RightTypeID = int.Parse(context.Request.Form["RightTypeID"]),
                SystemFlag = 1,
                CreateTime = DateTime.Now,
                CreateUser = 0,
                UpdateTime = DateTime.Parse("1900-01-01"),
                UpdateUser = 0
            };

            int rightID = IRights.AddRights(model);

        }

        private void update(HttpContext context)
        {
            int rightID = 0;
            int.TryParse(context.Request.Form["RightID"], out rightID);

            RightsModel model = IRights.GetByRightID(rightID);

            model.RightDisplayName = HttpContext.Current.Server.UrlDecode(context.Request.Form["RightDisplayName"]);
            model.RightMemo = HttpContext.Current.Server.UrlDecode(context.Request.Form["RightMemo"]) ?? "";
            model.RightName = HttpContext.Current.Server.UrlDecode(context.Request.Form["RightName"]);
            model.RightTypeID = int.Parse(context.Request.Form["RightTypeID"]);
            model.UpdateTime = DateTime.Now;
            IRights.UpdateByRightID(model);

        }

        private void delete(HttpContext context)
        {
            int rightID = 0;
            int.TryParse(context.Request.Form["RightID"], out rightID);
            IRights.DeleteByRightID(rightID);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
